// pages/search/search.js
import {getSearch} from "../../utils/api"

var fitList=[]
Page({

    /**
     * 页面的初始数据
     */
    
    data: {
        potelist:[],
        resList:[],
        n:10,
              
    },
    search:function(value) {
        this.getres(value)
        return new Promise((resolve,reject)=>{

        })
    },
    selectResult: function (e) {
        
        let value = e.currentTarget.dataset
        let name = value.text
        let author = value.author
        let content = value.content
        let dynasty = value.dynasty
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        console.log(prevPage);
        prevPage.setData({
            name:name,
            author:author,
            content:content,
            dynasty:dynasty
        })
        wx.navigateBack({
            delta: 1
        })

    },

    getres: function(e){
        getSearch({'vaule':e}).then(({res})=>{
            var result = res
            result.map(item=>{
                delete item.shangxi
                item.author=(item.author.split('，')[0]).split('（')[0]
    
            })
    
            this.setData({
                potelist:res
            })
            this.resultList()
        })

    },

    resultList(){
        fitList=[]
        let arr = this.data.potelist
        console.log(arr);
        let nums = this.data.n
        for(var i in arr){
            if(i<nums){

                fitList.push(arr[i])
            }
            
        }

        this.setData({
            resList:fitList
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom:async function() {
        wx.showLoading({
            title: '加载中',
        })
      


        let list = this.data.resList
        let len = list.length
        let tollist = this.data.potelist

        let addlist = tollist.slice(len,len+10)
        setTimeout(()=>{
            wx.hideLoading()
            this.setData({
            
            resList:this.data.resList.concat(addlist)
            })
        },1000)

        
        



    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            search: this.search.bind(this),
        }) 

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },



    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})